package com.hiss.basic.admin.controller;

import com.hiss.basic.admin.model.request.AuthenticationRequest;
import com.hiss.basic.admin.model.response.AuthenticationResponse;
import com.hiss.basic.admin.service.AuthenticationService;
import com.hiss.basic.starter.core.model.R;
import com.hiss.basic.starter.security.helper.SecurityHelper;
import com.hiss.basic.starter.security.model.AuthenticationModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private AuthenticationService authenticationService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public R<AuthenticationResponse> login(AuthenticationRequest request) {
        AuthenticationResponse response = authenticationService.login(request);
        return R.success(response);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public R<Void> logout() {
        authenticationService.logout();
        return R.success();
    }

    @RequestMapping(value = "/authentication", method = RequestMethod.GET)
    public R<AuthenticationModel> authentication() {
        return R.success(authenticationService.getAuthentication(SecurityHelper.getUserId()));
    }
}
